<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class UserFollow extends Model
{
    use SoftDeletes;
    protected $table = 'user_follows';

    /**
     * 定义主键
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 禁止批量赋值的字段
     *
     * @var array
     */
    protected $guarded = ['id','created_at','deleted_at'];

    /**
     * 软删除应该被调整为日期的属性
     * [$dates description]
     * @var [type]
     */
    protected $dates=[
        'created_at', 'updated_at','deleted_at'
    ];


    /*关联后台用户表*/
    public function followed_admin(){
        return $this -> hasOne(AdminUsers::class,'id','follow_admin_id');
    }

    /*关联前台用户表*/
    public function followed_user(){
        return $this -> hasOne(User::class,'id','follow_user_id');
    }


    /*被关注者的后台个人信息*/
    public static function adminUserInfo($admin_id){

        $article_num = Article::where('admin_id',$admin_id) -> where('is_admin',1) -> count();
        $res = AdminUsers::select('name as user_name','avatar','fans_num') -> find($admin_id);
        $res -> article_num = $article_num;
        $res -> person_profile = '';
        $res -> follow_num = UserFollow::where('follow_admin_id',$admin_id) -> where('is_admin',1) -> count();
        $res -> followed = UserFollow::where('follow_admin_id',$admin_id) -> where('is_admin',1) -> where('user_id',auth() -> id()) -> first() ? true : false;
        return $res;
    }


    /*被关注的前台用户信息*/
    public static function userInfo($user_id){
        $res = User::select('user_name','real_name','head_portrait as avatar','fans_num','article_num','follow_num','person_profile') -> find($user_id);
        $res -> avatar_url = config('app.img_url') . $res -> avatar;
        $res -> followed = UserFollow::where('user_id',auth() -> id()) -> where('follow_user_id',$user_id) -> first() ? true : false;
        return $res;
    }


    /*监测用户是否被关注*/
    public static function check($user_id,$followed_user_id,$is_admin = 1){
        $res  = self::where('user_id',$user_id) -> where('follow_'.($is_admin ? 'admin_id': 'user_id'),$followed_user_id) -> first();

        return $res ? true : false;
    }

    /*取消关注的用户 再次回关*/
    public static function back($user_id,$followed_user_id,$is_admin = 1){
        $res  = self::where('user_id',$user_id) -> where('follow_'.($is_admin ? 'admin_id': 'user_id'),$followed_user_id) -> first();

        if($res){
            $result = $res -> delete();
        }else{
            $followed_key = 'follow_'.($is_admin ? 'admin_id': 'user_id');
            $result = self::create(['user_id' => $user_id, $followed_key => $followed_user_id, 'is_admin' => 1]);
        }
        return $result;
    }

    // 关注或取消
    public static function followOrCancel($user_id,$followed_user_id,$is_admin = 1){
        $res  = self::where('user_id',$user_id) -> where('follow_'.($is_admin ? 'admin_id': 'user_id'),$followed_user_id)->where('is_admin',$is_admin)-> first();


        if($res){

            $result = $res -> delete();
            if($result){
                if($is_admin){
                    AdminUsers::where('id',$followed_user_id) -> decrement('fans_num');
                }else{
                    User::where('id',$followed_user_id) -> decrement('fans_num');
                }
            }
        }else{


            if($is_admin){

                $userFollow = [
                    'user_id' => auth() -> id(),
                    'is_admin' => '1',
                    'follow_admin_id' => $followed_user_id,
                ];
            }else{
                $userFollow = [
                    'user_id' => auth() -> id(),
                    'is_admin' => '0',
                    'follow_user_id' => $followed_user_id,
                ];
            }

            $result = UserFollow::create($userFollow);

            if($result){
                if($is_admin){
                    AdminUsers::where('id',$followed_user_id) -> increment('fans_num');
                }else{
                    User::where('id',$followed_user_id) -> increment('fans_num');
                }
            }
        }

        return $result;

    }










}
